package com.union.core.sys.dao.impl;

import java.util.*;
import java.sql.ResultSet;
import org.apache.log4j.Logger;
import com.hnjowe.webdb.*;
import com.union.core.sys.dao.SysModuleTypeDao;
import com.union.core.sys.model.*;

/**
 * DAO实现类: 系统模块类型
 * filename: SysModuleTypeDaoImpl.java
 * project:  CCTR
 * @company Hainan Jowe Technology Ltd
 * @author wangxiuwu
 * @create in 2009-5-21
 * @version 1.0
 */
public class SysModuleTypeDaoImpl implements SysModuleTypeDao{

	private DBOperation opera;
	private SysModuleType sysModuleType;
	private static Logger log = Logger.getLogger(SysModuleTypeDaoImpl.class.getName());

	public SysModuleTypeDaoImpl() {
	}

	/**
	 * 取出模块类型列表
	 * @return List
	 * @throws Exception
	 */
	public List<SysModuleType> getModuleTypeList() {
		List<SysModuleType> moduleTypeList = new ArrayList<SysModuleType>();
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "select "
					+ "MODULE_TYPE_ID,MODULE_TYPE_NAME,STATUS,ORDER_NUM "
					+ "from sys_module_type " + "order by ORDER_NUM";
			opera.prepStatment(sql);
			ResultSet res = opera.executeQuery();
			int i = 0;
			while (res.next()) {
				sysModuleType = new SysModuleType();
				sysModuleType.setMODULE_TYPE_ID(res.getInt(1));
				sysModuleType.setMODULE_TYPE_NAME(res.getString(2));
				sysModuleType.setSTATUS(res.getInt(3));
				sysModuleType.setORDER_NUM(res.getInt(4));
				moduleTypeList.add(i, sysModuleType);
				i++;
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return moduleTypeList;
	}

	/**
	 * 取出模块类型列表(取出有效1/无效0状态的记录)
	 * @param state int
	 * @return List
	 */
	public List<SysModuleType> getModuleTypeList(int state) {
		List<SysModuleType> moduleTypeList = new ArrayList<SysModuleType>();
		try {
			opera = new DBOperation();
			opera.transcationBegin();//建立连接并连接事物
			String sql = "select "
					+ "MODULE_TYPE_ID,MODULE_TYPE_NAME,STATUS,ORDER_NUM "
					+ "from sys_module_type " + "where STATUS=? "
					+ "order by ORDER_NUM";
			opera.prepStatment(sql);
			opera.setInt(1, state);
			ResultSet res = opera.executeQuery();//执行上面语句
			int i = 0;
			while (res.next()) {
				sysModuleType = new SysModuleType();
				sysModuleType.setMODULE_TYPE_ID(res.getInt(1));
				sysModuleType.setMODULE_TYPE_NAME(res.getString(2));
				sysModuleType.setSTATUS(res.getInt(3));
				sysModuleType.setORDER_NUM(res.getInt(4));
				moduleTypeList.add(i, sysModuleType);
				i++;
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}

		return moduleTypeList;
	}

	/**
	 * 修改模块类型数据1
	 * @param moduleTypeId int
	 * @param moduleTypeName String
	 * @param state int
	 * @return boolean
	 */
	public boolean modifyModuleType(int moduleTypeId, String moduleTypeName,
			int state) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "update sys_module_type "
					+ "set MODULE_TYPE_NAME=?,STATUS=? "
					+ "where MODULE_TYPE_ID=?";
			opera.prepStatment(sql);
			opera.setString(1, moduleTypeName);
			opera.setInt(2, state);
			opera.setInt(3, moduleTypeId);
			opera.executeUpdate();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

	/**
	 * 修改模块类型数据2
	 * @param moduleTypeId int
	 * @param state int
	 * @return boolean
	 */
	public boolean modifyModuleType(int moduleTypeId, int state) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();

			String sql = "update sys_module_type " + "set STATUS=? "
					+ "where MODULE_TYPE_ID=?";
			opera.prepStatment(sql);
			opera.setInt(1, state);
			opera.setInt(2, moduleTypeId);
			opera.executeUpdate();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

	/**
	 * 删除模块类型数据
	 * @param moduleTypeId int
	 * @return boolean
	 */
	public boolean deleteModuleType(int moduleTypeId) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "delete from sys_module_type "
					+ "where MODULE_TYPE_ID=?";
			opera.prepStatment(sql);
			opera.setInt(1, moduleTypeId);
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

	/**
	 * 添加模块类型数据
	 * @param moduleTypeBean ModuleTypeBean
	 * @return boolean
	 */
	public boolean addModuleType(SysModuleType moduleTypeBean) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "insert into sys_module_type"
					+ "(MODULE_TYPE_NAME,STATUS,ORDER_NUM) " 
					+ "values(?,?,?)";
			opera.prepStatment(sql);
			opera.setString(1, moduleTypeBean.getMODULE_TYPE_NAME());
			opera.setInt(2, moduleTypeBean.getSTATUS());
			opera.setInt(3, moduleTypeBean.getORDER_NUM());
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

	/**
	 * 模块类型分类排序
	 * @param ModuleTypeId int
	 * @param Type String
	 * @throws Exception
	 */
	public void orderModuleType(int ModuleTypeId, String Type) throws Exception {

		int pos = 0; //要排序栏目排序前所处位置
		int uppos = 0; //上一位置
		int downpos = 0; //下一位置

		List<SysModuleType> moduleTypeList = getModuleTypeList();
		int size = moduleTypeList.size();//模块类型个数
		for (int i = 0; i < size; i++) {
			SysModuleType moduleTypeBean = (SysModuleType) moduleTypeList
					.get(i);
			if (ModuleTypeId == moduleTypeBean.getMODULE_TYPE_ID()) {
				pos = i;//记录位置
				break;
			}
		}
		if ((size == 0) || (size == 1)) {//不能进行排序
			return;

		} else if (size == 2) {
			if (pos == 0) {
				uppos = pos;
				downpos = 1;
			} else {
				uppos = 0;
				downpos = pos;
			}

		} else {//栏目个数>=2
			if (pos == 0) { //在首部，只有降，无升
				uppos = pos = 0;
				downpos = 1;
			} else if (pos == size - 1) {//在末尾，只有升，无降
				uppos = pos - 1;
				downpos = pos;
			} else {
				uppos = pos - 1; //升一位
				downpos = pos + 1; //降一位
			}
		}

		SysModuleType moduleTypeBean;
		SysModuleType moduleTypeBean_up;
		SysModuleType moduleTypeBean_down;

		moduleTypeBean = (SysModuleType) moduleTypeList.get(pos);
		moduleTypeBean_up = (SysModuleType) moduleTypeList.get(uppos);
		moduleTypeBean_down = (SysModuleType) moduleTypeList.get(downpos);

		int moduleTypeId = moduleTypeBean.getMODULE_TYPE_ID();
		int order_num = moduleTypeBean.getORDER_NUM();

		if (Type.equals("up")) {//升
			int moduleTypeId_up = moduleTypeBean_up.getMODULE_TYPE_ID();
			int order_num_up = moduleTypeBean_up.getORDER_NUM();

			changeOrderNum_moduleType(moduleTypeId, order_num_up);
			changeOrderNum_moduleType(moduleTypeId_up, order_num);

		} else {//降
			int moduleTypeId_down = moduleTypeBean_down.getMODULE_TYPE_ID();
			int order_num_down = moduleTypeBean_down.getORDER_NUM();

			changeOrderNum_moduleType(moduleTypeId, order_num_down);
			changeOrderNum_moduleType(moduleTypeId_down, order_num);
		}
	}

	//改变排序号
	public boolean changeOrderNum_moduleType(int moduleTypeId, int newOrderNum) {
		boolean result = false;
		opera = new DBOperation();
		opera.transcationBegin();//建立连接并连接事物

		String sql = "update sys_module_type " + "set order_num=? "
				+ "where MODULE_TYPE_ID=?";

		opera.prepStatment(sql);
		opera.setInt(1, newOrderNum);
		opera.setInt(2, moduleTypeId);

		opera.executeUpdate();//执行上面语句
		result = opera.transcationCommit();

		opera.closePres();
		opera.closeConn();
		return result;
	}

	/**
	 * 自动获取排列序号
	 * @return int
	 */
	public int getOrderNumByAuto_moduleType() {
		int OrderNum = 1;
		try {
			List<Integer> OrderNumList = getOrderNumList_moduleType();
			int size = OrderNumList.size();
			if (size == 0) {
				OrderNum = 1;
			} else {
				OrderNum = (Integer) OrderNumList.get(0) + 1;
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return OrderNum;
	}

	//取出序号清单
	public List<Integer> getOrderNumList_moduleType() throws Exception {

		List<Integer> OrderNumList = new ArrayList<Integer>();
		try {
			opera = new DBOperation();
			opera.transcationBegin();//建立连接并连接事物
			String sql = "select ORDER_NUM " 
					+ "from sys_module_type "
					+ "order by ORDER_NUM desc";
			opera.prepStatment(sql);
			ResultSet res = opera.executeQuery();//执行上面语句
			int i = 0;
			while (res.next()) {
				OrderNumList.add(i, res.getInt(1));
				i++;
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return OrderNumList;
	}

	/**
	 * 提取信息
	 * @param MODULE_TYPE_ID int
	 * @return ModuleTypeBean
	 */
	public SysModuleType getModuleType(int MODULE_TYPE_ID) {

		SysModuleType sysModuleType = null;
		try {
			opera = new DBOperation();
			opera.transcationBegin();

			String sql = "select "
				+ "MODULE_TYPE_ID,MODULE_TYPE_NAME,STATUS,ORDER_NUM "
				+ "from sys_module_type " 
				+ "where MODULE_TYPE_ID=?";

			opera.prepStatment(sql);
			opera.setInt(1, MODULE_TYPE_ID);
			ResultSet res = opera.executeQuery();
			if (res.next()) {
				sysModuleType = new SysModuleType();
				sysModuleType.setMODULE_TYPE_ID(res.getInt(1));
				sysModuleType.setMODULE_TYPE_NAME(res.getString(2));
				sysModuleType.setSTATUS(res.getInt(3));
				sysModuleType.setORDER_NUM(res.getInt(4));
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return sysModuleType;
	}
	
}